package cn.edu.sdjzu.xg.eagle.dao;

import cn.edu.sdjzu.xg.eagle.entity.Department;
import cn.edu.sdjzu.xg.eagle.entity.School;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class DepartmentDaoImpl implements DepartmentDaoInterface {
    static private DepartmentDaoInterface departmentDaoImpl = null;
    public DepartmentDaoImpl(){}

    public static DepartmentDaoInterface getInstance(){
        if (departmentDaoImpl == null) {
            departmentDaoImpl = new DepartmentDaoImpl();
            System.out.println("Create DepartmentDaoImpl Object");
        }
        return departmentDaoImpl;
    }
    @Override
    public Department find(Long id) throws SQLException {
        //get connection
        Connection conn = JdbcHelper.getConn();
        //prepare statment
        PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Department WHERE id = ?");
        pstmt.setLong(1, id);
        //execute query
        ResultSet resultSet = pstmt.executeQuery();
        //analysis result set
        //create Department object(get School object)
        Department desiredDepartment = null;
        if (resultSet.next()) {
            String description = resultSet.getString("description");
            String no = resultSet.getString("no");
            String remarks = resultSet.getString("remarks");
            Long schoolId = resultSet.getLong("school_id");
            School school = SchoolDaoImpl.getInstance().find(schoolId);
            desiredDepartment = new Department(id, description, no, remarks, school);
        }

        return desiredDepartment;
    }



    @Override
    public boolean update(Department department) throws SQLException {
        return false;
    }

    @Override
    public boolean delete(Department department) throws SQLException {
        return false;
    }

    @Override
    public Collection<Department> findAll() throws SQLException, BaseException {
        return List.of();
    }

    @Override
    public boolean add(Department department) throws SQLException, BaseException {
        return false;
    }


    public boolean update(Connection connection,Department department)throws SQLException{
        String sql_update = "update Department set description = ?, no = ?, remarks = ? where id = ?";
        PreparedStatement pstmt_update = connection.prepareStatement(sql_update);
        pstmt_update.setString(1, department.getDescription());
        pstmt_update.setString(2, department.getNo());
        pstmt_update.setString(3, department.getRemarks());
        pstmt_update.setLong(4, department.getId());
        int rowAffected=pstmt_update.executeUpdate();
        JdbcHelper.close(pstmt_update, connection);
        return rowAffected>0;

    }

    public boolean delete(Long id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        String sql_delete = "delete from Department where id = ?";
        PreparedStatement pstmt_delete = connection.prepareStatement(sql_delete);
        pstmt_delete.setLong(1,id);
        int rowAffected=pstmt_delete.executeUpdate();
        JdbcHelper.close(pstmt_delete, connection);
        return rowAffected>0;

    }

    @Override
    public int countAll(School school) throws SQLException, BaseException {
        return 0;
    }

    public static void main(String[] args)throws SQLException{
        Connection connection = JdbcHelper.getConn();
        DepartmentDaoImpl departmentDaoImpl = new DepartmentDaoImpl();
        School school = new School(3L,"山东建筑大学","01","好");
        SchoolDaoImpl schoolDaoImol = new SchoolDaoImpl();
        Department department = new Department(6L,"管理工程学院","06","无",school);
        System.out.println(departmentDaoImpl.update(department));
        System.out.println(departmentDaoImpl.delete(department.getId()));
        System.out.println(departmentDaoImpl.find(7L));
    }
}
